package com.brainhealth.customer.service.impl;

import java.util.List;
import com.brainhealth.common.utils.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.brainhealth.customer.mapper.ChatMainMapper;
import com.brainhealth.customer.domain.ChatMain;
import com.brainhealth.customer.service.IChatMainService;

/**
 * 聊天主Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-04
 */
@Service
public class ChatMainServiceImpl implements IChatMainService 
{
    @Autowired
    private ChatMainMapper chatMainMapper;

    /**
     * 查询聊天主
     * 
     * @param id 聊天主主键
     * @return 聊天主
     */
    @Override
    public ChatMain selectChatMainById(Long id)
    {
        return chatMainMapper.selectChatMainById(id);
    }

    /**
     * 查询聊天主列表
     * 
     * @param chatMain 聊天主
     * @return 聊天主
     */
    @Override
    public List<ChatMain> selectChatMainList(ChatMain chatMain)
    {
        return chatMainMapper.selectChatMainList(chatMain);
    }

    /**
     * 新增聊天主
     * 
     * @param chatMain 聊天主
     * @return 结果
     */
    @Override
    public int insertChatMain(ChatMain chatMain)
    {
        chatMain.setCreateTime(DateUtils.getNowDate());
        return chatMainMapper.insertChatMain(chatMain);
    }

    /**
     * 修改聊天主表
     * @param chatMain 聊天主表
     * @return 结果
     */
    @Override
    public int onDuplicateUpdateChatMain(ChatMain chatMain)
    {
        ChatMain tmpChatMain=new ChatMain();
        tmpChatMain.setCustomerId(chatMain.getCustomerId());
        List<ChatMain> chatMainList = selectChatMainList(tmpChatMain);
        if (CollectionUtils.isEmpty(chatMainList)) {
            return insertChatMain(chatMain);
        } else {
            chatMain.setUpdateTime(DateUtils.getNowDate());
            chatMain.setId(chatMainList.get(0).getId());
            return chatMainMapper.updateChatMain(chatMain);
        }
    }

    @Override
    public int updateChatMain(ChatMain chatMain) {
        return chatMainMapper.updateChatMain(chatMain);
    }

    /**
     * 批量删除聊天主
     * 
     * @param ids 需要删除的聊天主主键
     * @return 结果
     */
    @Override
    public int deleteChatMainByIds(Long[] ids)
    {
        return chatMainMapper.deleteChatMainByIds(ids);
    }

    /**
     * 删除聊天主信息
     * 
     * @param id 聊天主主键
     * @return 结果
     */
    @Override
    public int deleteChatMainById(Long id)
    {
        return chatMainMapper.deleteChatMainById(id);
    }
}
